package wx_handler

import (
	"peilian-api/app/global/bind"
	"peilian-api/app/model"
	"peilian-api/app/services/wx_service"
	"peilian-api/utils/format"

	"github.com/gin-gonic/gin"
)

// 根据code获取用户手机号
func GetUserPhoneNumberByCode(ctx *gin.Context) {
	// 绑定参数
	var uc model.UserCode
	if err := bind.ShouldBinding(ctx, &uc); err != nil {
		format.NewResponseJson(ctx).Err(30001, err.Error())
		return
	}
	ws := new(wx_service.WxService)
	phone, err := ws.GetUserPhoneNumberByCode(uc.Code)
	if err != nil {
		format.NewResponseJson(ctx).Err(30002, err.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct {
		Phone string `json:"phone"`
	}{Phone: phone})
}

func Mn(ctx *gin.Context) {
	ctx.JSON(0, struct {
		Errcode   int64           `json:"errcode"`
		Errmsg    string          `json:"errmsg"`
		PhoneInfo model.PhoneInfo `json:"phone_info"`
	}{
		Errcode: 0,
		Errmsg:  "ok",
		PhoneInfo: model.PhoneInfo{
			PhoneNumber:     "15810507502",
			PurePhoneNumber: "15810507502",
			CountryCode:     "86",
			Watermark: model.WaterMark{
				TimeStamp: 1672126036,
				Appid:     "wx5f666cd6b33877ae",
			},
		},
	})
}

// 获取微信的jsapi签名

func GetJsapiSig(ctx *gin.Context) {
	// 绑定参数
	var req wx_service.JsApiSignatureReq
	if err := bind.ShouldBinding(ctx, &req); err != nil {
		format.NewResponseJson(ctx).Err(30001, err.Error())
		return
	}
	ws := new(wx_service.WxPaService)
	sig, err := ws.GetJsApiSignature(req)
	if err != nil {
		format.NewResponseJson(ctx).Err(30002, err.Error())
		return
	}
	format.NewResponseJson(ctx).SuccessObj(struct {
		Signature string `json:"signature"`
	}{Signature: sig})
}
